#include "manblue.h"
#include"gamecontrol.h"
ManBlue::ManBlue()
{
    this->setPixmap(QPixmap(":/1/Image/ManBlueStand.png"));
    this->setPos(20,879);
}

void ManBlue::Move()
{
    for(int i=0;i< GameControl::Instance()->mKeyList.size();i++)
    {
        switch( GameControl::Instance()->mKeyList[i])
        {
        case Qt::Key_W:
        {
            this->setPixmap(QPixmap(":/1/Image/ManBlueUp.png"));
            jump();
            break;
        }
        case Qt::Key_S:
        {
            this->setPixmap(QPixmap(":/1/Image/ManBlueDown.png"));
            down();
            break;
        }
        case Qt::Key_A:
        {
            this->setPixmap(QPixmap(":/1/Image/ManBlueLeft.png"));
            this->moveBy(-1 * mMoveSpeed,0);
            break;
        }
        case Qt::Key_D:
        {
            this->setPixmap(QPixmap(":/1/Image/ManBlueRight.png"));
            this->moveBy(1*mMoveSpeed,0);
            break;
        }
        }
    }
    //检测碰到边界
    boundaryJudge();
    //检测碰到水
    watergreenJudge(20,879);
    water();
}

void ManBlue::updatePosition()
{
    if(isJumping)
    {
        vy -= gravity;
        this->setY(this->y()-vy);
    }

    if(this->y()>GameDefine::ScreenHeight-this->boundingRect().height()-30 )
    {
        this->setY(GameDefine::ScreenHeight-this->boundingRect().height()-30);
        vy = 0;
        isJumping = false;
    }

    QRectF mPlayerRect2 = QRectF(this->x()+20,this->y()+25,50,5);

    for(int i=0;i<GameControl::Instance()->platforms.size();i++)
    {
        if(mPlayerRect2.intersects(GameControl::Instance()->platforms[i]))
        {
            vy = -vy;
            break;
        }
    }
}

void ManBlue::detectCollisions()//检测下部分碰撞
{
    bool onPlatform = false;

    QRectF mPlayerRect1 = QRectF(this->x()+30,this->y()+80,25,1);

    for(int i=0 ;i<GameControl::Instance()->platforms.size();i++)
    {
        if(mPlayerRect1.intersects(GameControl::Instance()->platforms[i]))
        {
            this->setY(GameControl::Instance()->platforms[i].y()-this->boundingRect().height()+20);
            vy = 0;
            isJumping = false;
            onPlatform = true;
            break;
        }
    }

    if(!onPlatform)
    {
        down();//检测人物是否在平台上 不在就掉下来
    }
}

void ManBlue::diamondCollisions()//钻石碰撞
{
    QRectF mManBlueRect = QRectF(this->x()+25,this->y()+25,50,50);

    for(int i=0;i<GameControl::Instance()->mDiamondsBlue.size();i++)
    {
        if(mManBlueRect.intersects(GameControl::Instance()->GameControl::Instance()->mDiamondsBlue[i]->_Rect))
        {
            QGraphicsScene& gameScene= GameControl::Instance()->getGameScene();
            gameScene.removeItem(GameControl::Instance()->mDiamondsBlue[i]);
            delete GameControl::Instance()->mDiamondsBlue[i];
            GameControl::Instance()->mDiamondsBlue.removeAt(i);
            i--;
        }
    }
}

void ManBlue::water()//红水
{
    if(this->x()>=586 && this->x()<=700 && this->y() >=875 && this->y()<=880)
    {
        this->setPos(20,879);
    }
}

void ManBlue::changePixmap()//换图片
{
    if( GameControl::Instance()->mKeyList.size() == 0)
    {
        this->setPixmap((QPixmap(":/1/Image/ManBlueStand.png")));
    }
}

ManBlue::~ManBlue()
{

}

